package com.yaodian100.ec.member.web;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.logger.service.LogService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.entity.Option;
import com.yaodian100.ec.member.entity.BlackList;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.MemberService;

public class BlackListAction extends ActionSupport implements Preparable {
	private static final long serialVersionUID = 178887580319281383L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(BlackListAction.class);

	@Autowired
	private LogService logService;

	private MemberService memberService;
	private DomainService<Menu> menuService;
	private BlackList blackList;
	private List<BlackList> blackLists;
	private String memberId;
	private String blackListId;
	private String blockType;
	private String blockStatus;
	private String blockReason;
	private Menu blackListStatus;
	private Menu memberBlockStatus;
	private String blockDataExist;
	private String saveResult = "";
	private Member member;
	private CommonCriteria crit = null;

	/**
	 * 新增黑名单
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		blackList = new BlackList();
		blackListId = "";
		return "edit";
	}

	/**
	 * 资料栏位验证
	 * 
	 * @return
	 * @throws CoreException
	 */
	public boolean blackListValidate() throws CoreException {
		if (StringUtils.isBlank(memberId)) {
			addActionError("传入参数错误");
		} else {
			if (StringUtils.isNotBlank(blackListId))
				blackList = memberService.getBlackList(Integer.parseInt(blackListId));
			else
				blackList = new BlackList();

			blackList.setMemberId(memberId);
			/**************************** 必填栏位 ****************************/
			if (StringUtils.isBlank(blockType))
				addFieldError("blockType", "请选择黑名单等级");
			else
				blackList.setBlockType(blockType);

			if (StringUtils.isBlank(blockStatus))
				addFieldError("blockStatus", "请输入黑名单状态");
			else
				blackList.setBlockStatus(blockStatus);

			/**************************** 非必填栏位 ****************************/
			if (StringUtils.isNotBlank(blockReason))
				blackList.setBlockReason(blockReason);

		}
		if (this.getFieldErrors().isEmpty() || this.getActionErrors().isEmpty())
			return true;
		else
			return false;
	}

	/**
	 * 验证黑名单内是否有启用的状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkBlockStatus() throws Exception {
		crit = new CommonCriteria();
		crit.addEq("blockStatus", "Y");
		crit.addEq("memberId", memberId);
		blackLists = memberService.getBlackLists(crit);
		if (blackLists.isEmpty())
			blockDataExist = "N";
		else
			blockDataExist = "Y";
		return "jsonCheckBlockStatus";
	}

	/**
	 * 编辑黑名单
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		if (StringUtils.isNotBlank(blackListId))
			blackList = memberService.getBlackList(Integer.parseInt(blackListId));
		return "edit";
	}

	/**
	 * @return the blackList
	 */
	public BlackList getBlackList() {
		return blackList;
	}

	/**
	 * @return the blackListId
	 */
	public String getBlackListId() {
		return blackListId;
	}

	/**
	 * @return the blackLists
	 */
	public List<BlackList> getBlackLists() {
		return blackLists;
	}

	public Menu getBlackListStatus() {
		return blackListStatus;
	}

	/**
	 * @return the blockDataExist
	 */
	public String getBlockDataExist() {
		return blockDataExist;
	}

	/**
	 * @return the blockReason
	 */
	public String getBlockReason() {
		return blockReason;
	}

	/**
	 * @return the blockStatus
	 */
	public String getBlockStatus() {
		return blockStatus;
	}

	public String getBlockType() {
		return blockType;
	}

	public Member getMember() {
		return member;
	}

	/**
	 * @return the memberBlockStatus
	 */
	public Menu getMemberBlockStatus() {
		return memberBlockStatus;
	}

	public String getMemberId() {
		return memberId;
	}

	public String getSaveResult() {
		return saveResult;
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (null == blackListStatus) {
			Menu menu = menuService.get("Member.Status");
			blackListStatus = new Menu();
			for (Option op : menu.getOptions().values()) {
				if (!"00".equals(op.getCode()))
					blackListStatus.addOption(op.getCode(), op.getName());
			}
		}
		if (null == memberBlockStatus) {
			memberBlockStatus = menuService.get("Member.BlockStatus");
		}
		logger.info("prepare ok");
	}

	/**
	 * 新增/修改锁定会员状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			if (blackListValidate()) {
				if (StringUtils.isBlank(blackListId))
					blackList = memberService.saveBlackList(blackList);
				else
					blackList = memberService.updateBlackList(blackList);
			}
		} catch (CoreException e) {
			addActionError(e.getMessage());
			setSaveResult(e.getMessage().toString());
		}
		return "jsonSaveBlackList";
	}

	public String search() throws Exception {
		return "list";
	}

	/**
	 * @param blackList the blackList to set
	 */
	public void setBlackList(BlackList blackList) {
		this.blackList = blackList;
	}

	/**
	 * @param blackListId the blackListId to set
	 */
	public void setBlackListId(String blackListId) {
		this.blackListId = blackListId;
	}

	/**
	 * @param blackLists the blackLists to set
	 */
	public void setBlackLists(List<BlackList> blackLists) {
		this.blackLists = blackLists;
	}

	public void setBlackListStatus(Menu blackListStatus) {
		this.blackListStatus = blackListStatus;
	}

	/**
	 * @param blockDataExist the blockDataExist to set
	 */
	public void setBlockDataExist(String blockDataExist) {
		this.blockDataExist = blockDataExist;
	}

	/**
	 * @param blockReason the blockReason to set
	 */
	public void setBlockReason(String blockReason) {
		this.blockReason = blockReason;
	}

	/**
	 * @param blockStatus the blockStatus to set
	 */
	public void setBlockStatus(String blockStatus) {
		this.blockStatus = blockStatus;
	}

	public void setBlockType(String blockType) {
		this.blockType = blockType;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	/**
	 * @param memberBlockStatus the memberBlockStatus to set
	 */
	public void setMemberBlockStatus(Menu memberBlockStatus) {
		this.memberBlockStatus = memberBlockStatus;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	/**
	 * @param menuService the menuService to set
	 */
	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public void setSaveResult(String saveResult) {
		this.saveResult = saveResult;
	}

	/**
	 * 会员状态快速解除锁定
	 * 
	 * @return
	 * @throws Exception
	 */
	public String unBlock() throws Exception {
		logger.info("blackListId");
		blackList = memberService.getBlackList(Integer.parseInt(blackListId));
		blackList.setBlockStatus("N");
		blackList = memberService.quickUnLock(blackList);
		member = memberService.get(blackList.getMemberId());
		logService.save(getClass().getSimpleName(), "unBlock", blackList);
		// return "list";
		return "jsonUnBlock";
	}

	/**
	 * 修改会员状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		blackList = memberService.getBlackList(Integer.parseInt(blackListId));
		blackList.setBlockReason(blockReason);
		blackList.setBlockStatus(blockStatus);
		blackList.setBlockType(blockType);
		blackList = memberService.updateBlackList(blackList);
		return "list";
	}
}